package com.wjj.common.mybatis.config;

import com.mybatisflex.core.keygen.KeyGeneratorFactory;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import com.wjj.common.mybatis.core.generator.SnowFlakeIdGenerator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author wujunjie
 * @version 1.0
 * @date 2025/07/30 - 9:39
 */
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan("${mybatis-flex.mapperPackage}")
public class MybatisFlexAutoConfiguration {

    public MybatisFlexAutoConfiguration() {
        // 自定义ID生成器
        KeyGeneratorFactory.register("mySnowFlakeId", new SnowFlakeIdGenerator());
    }

    /**
     * 自定义mybatis-flex配置
     * @return
     */
    @Bean
    public MyBatisFlexCustomizer myBatisFlexCustomizer() {
        return new MybatisFlexConfig();
    }

    /**
     * 控制台打印sql执行语句和耗时
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "spring.profiles", name = "active", havingValue = "dev")
    public MyBatisFlexPrintLogConfig myBatisFlexPrintLogConfig() {
        return new MyBatisFlexPrintLogConfig();
    }
}
